﻿//using System.Reflection;
//using AutoMapper;
//using Microsoft.Extensions.DependencyInjection;
//using Service.Framework.Utils.Extension;

//namespace Service.Core.AutoMapper;

//public static class AutoMapperxtensions
//{
//    static AutoMapperxtensions()
//    {
//        _loadedAssemblies = new List<Assembly>();
//    }

//    private static IEnumerable<Assembly> _loadedAssemblies;

//    /// <summary>
//    /// 使用AutoMapper自动映射拥有MapAttribute的类
//    /// </summary>
//    /// <param name="services">服务集合</param>
//    /// <param name="configure">自定义配置</param>
//    public static IServiceCollection AddAutoMapper(this IServiceCollection services,
//        Action<IMapperConfigurationExpression> configure = null)
//    {
//        if (!_loadedAssemblies.Any())
//        {
//            LoadAssemblies();
//        }
//        var allFxTypes = _loadedAssemblies.SelectMany(s => s.GetTypes()).ToList();
//        List<(Type from, Type[] targets)> maps = new List<(Type from, Type[] targets)>();
//        maps.AddRange(allFxTypes.Where(x => x.GetCustomAttribute<MapAttribute>() != null)
//            .Select(x => (x, x.GetCustomAttribute<MapAttribute>().TargetTypes)));
//        var configuration = new MapperConfiguration(cfg =>
//        {
//            maps.ForEach(aMap =>
//            {
//                aMap.targets.ToList().ForEach(aTarget =>
//                {
//                    Console.WriteLine(aMap.from.Name + "----------", aTarget.Name);

//                    cfg.CreateMap(aMap.from, aTarget)
//                        .IgnoreAllNonExisting(aMap.from, aTarget)
//                        .ReverseMap();
//                });
//            });
//            cfg.AddMaps(_loadedAssemblies); // 加载全部程序集 不仅仅是为先前所写的Map属性映射 也是为了AutoMapper的属性映射功能
//            //自定义映射
//            configure?.Invoke(cfg);
//        });
//        services.AddSingleton(configuration.CreateMapper());

//        return services;
//    }

//    /// <summary>
//    /// 忽略所有不匹配的属性。
//    /// </summary>
//    /// <param name="expression">配置表达式</param>
//    /// <param name="from">源类型</param>
//    /// <param name="to">目标类型</param>
//    /// <returns></returns>
//    private static IMappingExpression IgnoreAllNonExisting(this IMappingExpression expression, Type from, Type to)
//    {
//        var flags = BindingFlags.Public | BindingFlags.Instance;
//        to.GetProperties(flags).Where(x => from.GetProperty(x.Name, flags) == null).ForEach(aProperty =>
//        {
//            expression.ForMember(aProperty.Name, opt => opt.Ignore());
//        });

//        return expression;
//    }

//    private static void LoadAssemblies(params string[] scanAssembliesStartsWith)
//    {
//        List<Assembly> list = new List<Assembly>();
//        List<string> list2 = new List<string>();
//        string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
//        if (scanAssembliesStartsWith?.Any() ?? false)
//        {
//            if (scanAssembliesStartsWith.Length == 1)
//            {
//                string searchPattern = scanAssembliesStartsWith.First() + "*.dll";
//                string[] files = Directory.GetFiles(baseDirectory, searchPattern);
//                list2.AddRange(files);
//            }

//            if (scanAssembliesStartsWith.Length > 1)
//            {
//                foreach (string text in scanAssembliesStartsWith)
//                {
//                    string searchPattern2 = text + "*.dll";
//                    string[] files2 = Directory.GetFiles(baseDirectory, searchPattern2);
//                    list2.AddRange(files2);
//                }
//            }
//        }
//        else
//        {
//            string[] files3 = Directory.GetFiles(baseDirectory, "*.dll");
//            list2.AddRange(files3);
//        }


//        foreach (string item2 in list2)
//        {
//            try
//            {
//                Assembly item = Assembly.LoadFrom(item2);
//                list.Add(item);
//            }
//            catch (Exception)
//            {
//                // ignored
//            }
//        }

//        _loadedAssemblies = list;
//    }
////}